package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.BusinessDemand;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.domain.request.RequestBusinessDemand;
import com.innovations.inn.domain.request.RequestListFilterPreview;
import com.innovations.inn.domain.request.RequestListPreview;
import com.innovations.inn.domain.response.ResponseBusinessDemandList;
import com.innovations.inn.domain.response.ResponseBusinessFilterDemand;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.service.IBusinessDemandService;
import com.innovations.inn.service.IResearchInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商业需求Controller
 *
 * @author dp
 * @date 2024-08-23
 */
@Api(tags = "商业需求")
@RestController
@RequestMapping("/inn/demand")
public class BusinessDemandController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    IResearchInstitutionService researchInstitutionService;
    @Autowired
    private IBusinessDemandService businessDemandService;

    /**
     * 查询商业需求列表
     */
    @ApiOperation("查询商业需求列表")
    @PreAuthorize("@ss.hasPermi('inn:demand:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessDemand businessDemand) {
        startPage();
        List<ResponseBusinessDemandList> list = businessDemandService.selectBusinessDemandList(businessDemand);
        return getDataTable(list);
    }

    /**
     * 查询商业需求列表合计
     */
    @ApiOperation("查询商业需求列表合计")
    @PreAuthorize("@ss.hasPermi('inn:demand:list')")
    @GetMapping("/list/count")
    public TableDataInfo listCount(BusinessDemand businessDemand) {
        startPage();
        List<ResponseItemCount> listCount = businessDemandService.selectBusinessDemandCount(businessDemand);
        return getDataTable(listCount);
    }

    /**
     * 查询商业需求列表-预览展示
     */
    @ApiOperation("查询商业需求列表-预览展示")
    @PreAuthorize("@ss.hasPermi('inn:demand:list')")
    @GetMapping("/list/preview")
    public TableDataInfo preview(HttpServletRequest request, @RequestBody RequestListPreview businessDemand) {
        if (businessDemand.getUserId() == null) {
            businessDemand.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        }
        startPage();
        List<BusinessDemand> list = businessDemandService.selectBusinessDemandPreview(businessDemand);
        return getDataTable(list);
    }

    /**
     * 查询商业需求列表-预览展示-过滤
     */
    @ApiOperation("查询商业需求列表-预览展示-过滤")
    @PreAuthorize("@ss.hasPermi('inn:demand:list')")
    @PostMapping("/filter/list/preview")
    public TableDataInfo filterPreview(HttpServletRequest request, @RequestBody RequestListFilterPreview businessDemand) {
        if (businessDemand.getUserId() == null) {
            businessDemand.setUserId(tokenService.getLoginUser(request).getUser().getUserId());
        }
        startPage();
        List<ResponseBusinessFilterDemand> list = businessDemandService.selectBusinessDemandFilterPreview(businessDemand);
        return getDataTable(list);
    }

    /**
     * 查询用户待审核列表
     */
    @ApiOperation("查询用户待审核列表")
    @PreAuthorize("@ss.hasPermi('inn:outcomes:list')")
    @GetMapping("/user/list")
    public TableDataInfo userList(Long userId, Long state) {
        ResearchInstitution researchOutcomes = new ResearchInstitution();
        researchOutcomes.setUserId(userId);
        List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchOutcomes);
        if (list == null || list.isEmpty()) {
            throw new RuntimeException("当前用户无可管理机构");
        }
        RequestListFilterPreview roInput = new RequestListFilterPreview();

        roInput.setInstitutionId(list.get(0).getInstitutionId());

        if (state != null) {
            roInput.setState(state);
        }

        startPage();
        List<ResponseBusinessFilterDemand> Returnlist = businessDemandService.selectBusinessDemandFilterPreview(roInput);
        return getDataTable(Returnlist);
    }

    /**
     * 导出商业需求列表
     */
    @PreAuthorize("@ss.hasPermi('inn:demand:export')")
    @Log(title = "商业需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessDemand businessDemand) {
        List<ResponseBusinessDemandList> list = businessDemandService.selectBusinessDemandList(businessDemand);
        ExcelUtil<ResponseBusinessDemandList> util = new ExcelUtil<>(ResponseBusinessDemandList.class);
        util.exportExcel(response, list, "商业需求数据");
    }

    /**
     * 获取商业需求详细信息
     */
    @ApiOperation("获取商业需求详细信息")
    @PreAuthorize("@ss.hasPermi('inn:demand:query')")
    @GetMapping(value = "/{businessDemandId}")
    public AjaxResult getInfo(@PathVariable("businessDemandId") Long businessDemandId) {
        return success(businessDemandService.selectBusinessDemandByBusinessDemandId(businessDemandId));
    }

    /**
     * 新增商业需求
     */
    @ApiOperation("新增商业需求")
    @PreAuthorize("@ss.hasPermi('inn:demand:add')")
    @Log(title = "商业需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody RequestBusinessDemand businessDemand) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessDemandService.completeData(businessDemand, userId, 1);
        return toAjax(businessDemandService.insertBusinessDemand(businessDemand));
    }

    /**
     * 修改商业需求
     */
    @ApiOperation("修改商业需求")
    @PreAuthorize("@ss.hasPermi('inn:demand:edit')")
    @Log(title = "商业需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody RequestBusinessDemand businessDemand) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        businessDemandService.completeData(businessDemand, userId, 2);
        return toAjax(businessDemandService.updateBusinessDemand(businessDemand));
    }

    /**
     * 删除商业需求
     */
    @ApiOperation("删除商业需求")
    @PreAuthorize("@ss.hasPermi('inn:demand:remove')")
    @Log(title = "商业需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{businessDemandIds}")
    public AjaxResult remove(@PathVariable Long[] businessDemandIds) {
        return toAjax(businessDemandService.deleteBusinessDemandByBusinessDemandIds(businessDemandIds));
    }
}
